package com.forest.platform.service.example.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.forest.constants.EncryptionAlgorithm;
import com.forest.dto.AuthDTO;
import com.forest.dto.AuthEncryptionDTO;
import com.forest.entity.SysAppInfo;
import com.forest.service.SignatureService;
import com.forest.platform.service.example.service.SignatureTestService;
import com.forest.service.SysAppInfoService;
import com.forest.util.EncryptionKeyUtil;
import com.forest.util.EnvironmentHolder;
import com.forest.util.SignParamUtil;
import com.forest.util.Sm4Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class SignatureTestServiceImpl implements SignatureTestService {

    @Resource
    private SignatureService signatureService;

    @Resource
    private SysAppInfoService sysAppInfoService;

    @Override
    public String testVerifySign(AuthDTO dto) {
        SysAppInfo appInfo = sysAppInfoService.getByAppKey(dto.getAppKey());
        Assert.notNull(appInfo, "应用信息不存在");
        //校验签名
        Assert.isTrue(signatureService.verify(dto), "签名校验失败");
        //带app参数校验
        Assert.isTrue(signatureService.verify(dto, appInfo), "签名校验失败");
        //处理业务
        String extParam = SignParamUtil.getExtParam(dto.getExtra(), String.class);
        log.info("业务参数: {}", extParam);
        JSONObject jsonObject = JSONObject.parseObject(dto.getExtra());
        String phone = jsonObject.getString("phone");
        String decryptPhone = Sm4Util.decrypt(phone, sysAppInfoService.decryptKey(appInfo));
        log.info("解密后电话号码: {}", decryptPhone);
        return "验证成功";
    }

    @Override
    public String testVerifySignFoeEncryption(AuthEncryptionDTO dto) {
        SysAppInfo appInfo = sysAppInfoService.getByAppKey(dto.getAppKey());
        Assert.notNull(appInfo, "应用信息不存在");
        //校验签名
        Assert.isTrue(signatureService.verify(dto), "签名校验失败");
        //带app参数校验
        Assert.isTrue(signatureService.verify(dto, appInfo), "签名校验失败");
        //处理业务
        String secretKey = StrUtil.equals(StrUtil.isBlank(appInfo.getEncryptionAlgorithm()) ? EnvironmentHolder.getEncryptionAlgorithm() : appInfo.getEncryptionAlgorithm(), EncryptionAlgorithm.SM4.getAlgorithm()) ? appInfo.getAppSecretSm4() : appInfo.getAppSecretAes();
        int type = StrUtil.equals(StrUtil.isBlank(appInfo.getEncryptionAlgorithm()) ? EnvironmentHolder.getEncryptionAlgorithm() : appInfo.getEncryptionAlgorithm(), EncryptionAlgorithm.SM4.getAlgorithm()) ? 1 : 2;
        String appSecretKey = EncryptionKeyUtil.getAppSecretKey(type, secretKey);
        //解密获取参数
        String extParam = SignParamUtil.getExtParam(dto, StrUtil.isBlank(appInfo.getEncryptionAlgorithm()) ? EnvironmentHolder.getEncryptionAlgorithm() : appInfo.getEncryptionAlgorithm(), appSecretKey, String.class);
        //处理业务
        log.info("业务参数: {}", JSONUtil.toJsonStr(extParam));
        return "验证成功";
    }
}
